<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动类型" prop="hdlxId">
        <el-select v-model="queryParams.hdlxId"
                   placeholder="请选择下拉选择"
                   clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in hudongList"
                     :key="index"
                     :label="item.hdlxName"
                     :value="item.hdlxId"
                     :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="活动细则" prop="sjbmHdxz">
        <el-input
          v-model="queryParams.sjbmHdxz"
          placeholder="请输入活动细则"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="活动开始">
        <el-date-picker
          v-model="value1"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="活动结束">
        <el-date-picker
          v-model="value2"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="报名开始">
        <el-date-picker
          v-model="value3"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="报名结束">
        <el-date-picker
          v-model="value4"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="状态" prop="sjbmZt">
        <el-select v-model="queryParams.sjbmZt" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sjbm_zt"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="报名量" style="width: 130px;margin-right: 30px">
        <el-input
          v-model="value5"
          placeholder="请输入最小值"
          clearable
          @keyup.enter.native="handleQuery"
        />
        <el-input
          v-model="value6"
          placeholder="请输入最大值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:shangjiabaoming:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:shangjiabaoming:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:shangjiabaoming:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:shangjiabaoming:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shangjiabaomingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="活动id" width="75" align="center" prop="sjbmHdid" />
      <el-table-column label="活动类型" width="220" align="center" prop="hudongleixing.hdlxName" />
      <el-table-column label="活动细则" width="220" align="center" prop="sjbmHdxz" />
      <el-table-column label="活动日期起始时间" align="center" prop="sjbmHdStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sjbmHdStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动日期结束时间" align="center" prop="sjbmHdEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sjbmHdEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名日期起始时间" align="center" prop="sjbmBmStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sjbmBmStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名日期结束时间" align="center" prop="sjbmBmEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sjbmBmEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="店铺类型要求" width="120" align="center" prop="sjbmDpyq" />
      <el-table-column label="应用范围" align="center" prop="sjbmFw" />
      <el-table-column label="报名费" align="center" prop="sjbmBmf" />
      <el-table-column label="报名量" align="center" prop="sjbmBml" />
      <el-table-column label="创建时间" align="center" prop="sjbmCreatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sjbmCreatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="sjbmZt">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sjbm_zt" :value="scope.row.sjbmZt"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:shangjiabaoming:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:shangjiabaoming:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商家报名活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动类型id" prop="hdlxId">
          <el-select v-model="form.hdlxId"
                     placeholder="请选择下拉选择"
                     clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in hudongList"
                       :key="index"
                       :label="item.hdlxName"
                       :value="item.hdlxId"
                       :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动细则" prop="sjbmHdxz">
          <el-input v-model="form.sjbmHdxz" placeholder="请输入活动细则" />
        </el-form-item>
        <el-form-item label="活动日期起始时间" prop="sjbmHdStart">
          <el-date-picker clearable
            v-model="form.sjbmHdStart"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择活动日期起始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动日期结束时间" prop="sjbmHdEnd">
          <el-date-picker clearable
            v-model="form.sjbmHdEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择活动日期结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名日期起始时间" prop="sjbmBmStart">
          <el-date-picker clearable
            v-model="form.sjbmBmStart"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择报名日期起始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名日期结束时间" prop="sjbmBmEnd">
          <el-date-picker clearable
            v-model="form.sjbmBmEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择报名日期结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="店铺类型要求" prop="sjbmDpyq">
          <el-input v-model="form.sjbmDpyq" placeholder="请输入店铺类型要求" />
        </el-form-item>
        <el-form-item label="应用范围" prop="sjbmFw">
          <el-input v-model="form.sjbmFw" placeholder="请输入应用范围" />
        </el-form-item>
        <el-form-item label="报名费" prop="sjbmBmf">
          <el-input v-model="form.sjbmBmf" placeholder="请输入报名费" />
        </el-form-item>
        <el-form-item label="报名量" prop="sjbmBml">
          <el-input v-model="form.sjbmBml" placeholder="请输入报名量" />
        </el-form-item>
        <el-form-item label="创建时间" prop="sjbmCreatetime">
          <el-date-picker clearable
            v-model="form.sjbmCreatetime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="sjbmZt">
          <el-select v-model="form.sjbmZt" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.sjbm_zt"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listShangjiabaoming,
  getShangjiabaoming,
  delShangjiabaoming,
  addShangjiabaoming,
  updateShangjiabaoming,
  listhdlxname
} from "@/api/system/shangjiabaoming";

export default {
  name: "Shangjiabaoming",
  dicts: ['sjbm_zt'],
  data() {
    return {
      value1:'',
      value2:'',
      value3:'',
      value4:'',
      value5:null,
      value6:null,
      //查询活动类型下拉
      hudongList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商家报名活动表格数据
      shangjiabaomingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 创建时间时间范围
      daterangeSjbmHdStart: [],
      // 创建时间时间范围
      daterangeSjbmHdEnd: [],
      // 创建时间时间范围
      daterangeSjbmBmStart: [],
      // 创建时间时间范围
      daterangeSjbmBmEnd: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hdlxId: null,
        hdlxName: null,
        sjbmHdxz: null,
        sjbmHdStart: null,
        sjbmHdEnd: null,
        sjbmBmStart: null,
        sjbmBmEnd: null,
        sjbmDpyq: null,
        sjbmFw: null,
        sjbmBmf: null,
        sjbmBml: null,
        sjbmCreatetime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hdlxId: [
          { required: true, message: "活动类型id不能为空", trigger: "blur" }
        ],
        sjbmZt: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ]

      }

    };
  },
  created() {
    this.getList();
    this.hudongleixing();
  },
  methods: {
    /** 查询商家报名活动列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};

      if (null != this.value1 && '' != this.value1) {
        this.queryParams.params["beginSjbmHdStart"] = this.getGoodTime(this.value1[0]);
        this.queryParams.params["endSjbmHdStart"] = this.getGoodTime(this.value1[1]);
      }
      if (null != this.value2 && '' != this.value2) {
        this.queryParams.params["beginSjbmHdEnd"] = this.getGoodTime(this.value2[0]);
        this.queryParams.params["endSjbmHdEnd"] = this.getGoodTime(this.value2[1]);
      }
      if (null != this.value3 && '' != this.value3) {
        this.queryParams.params["beginSjbmBmStart"] = this.getGoodTime(this.value3[0]);
        this.queryParams.params["endSjbmBmStart"] = this.getGoodTime(this.value3[1]);
      }
      if (null != this.value4 && '' != this.value4) {
        this.queryParams.params["beginSjbmBmEnd"] = this.getGoodTime(this.value4[0]);
        this.queryParams.params["endSjbmBmEnd"] = this.getGoodTime(this.value4[1]);
      }
      if (null != this.value5 && '' != this.value5 && null != this.value6 && '' != this.value6){
        this.queryParams.params["bmlmin"] = this.value5;
        this.queryParams.params["bmlmax"] = this.value6;
      }
      console.log(this.queryParams.params);
      listShangjiabaoming(this.queryParams).then(response => {
        this.shangjiabaomingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    hudongleixing() {
      listhdlxname(null).then(response => {
        this.hudongList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sjbmHdid: null,
        hdlxId: null,
        sjbmHdxz: null,
        sjbmHdStart: null,
        sjbmHdEnd: null,
        sjbmBmStart: null,
        sjbmBmEnd: null,
        sjbmDpyq: null,
        sjbmFw: null,
        sjbmBmf: null,
        sjbmBml: null,
        sjbmCreatetime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeSjbmHdStart = [];
      this.daterangeSjbmHdEnd = [];
      this.daterangeSjbmBmStart = [];
      this.daterangeSjbmBmEnd = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sjbmHdid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商家报名活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sjbmHdid = row.sjbmHdid || this.ids
      getShangjiabaoming(sjbmHdid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商家报名活动";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sjbmHdid != null) {
            updateShangjiabaoming(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShangjiabaoming(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sjbmHdids = row.sjbmHdid || this.ids;
      this.$modal.confirm('是否确认删除商家报名活动编号为"' + sjbmHdids + '"的数据项？').then(function() {
        return delShangjiabaoming(sjbmHdids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/shangjiabaoming/export', {
        ...this.queryParams
      }, `shangjiabaoming_${new Date().getTime()}.xlsx`)
    },

    //时间格式转化
    getGoodTime(time) {
      // 尝试将输入字符串解析为Date对象
      let date = new Date(time);
      // 检查Date对象是否有效（即不是Invalid Date）
      if (isNaN(date.getTime())) {
        throw new Error('Invalid date string');
      }
      // 提取年、月、日、时、分、秒
      // 月份从0开始，所以要加1；日期、小时、分钟、秒直接使用
      // 使用padStart来确保月份、日期、小时、分钟、秒始终是两位数
      const year = time.getFullYear();
      const month = String(time.getMonth() + 1).padStart(2, '0');
      const day = String(time.getDate()).padStart(2, '0');
      const hours = String(time.getHours()).padStart(2, '0');
      const minutes = String(time.getMinutes()).padStart(2, '0');
      const seconds = String(time.getSeconds()).padStart(2, '0');

      // 拼接成所需的格式
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }
  },


};
</script>
